package cn.bdqn.service.impl;

import cn.bdqn.mapper.SysUserMapper;
import cn.bdqn.pojo.SysUser;
import cn.bdqn.service.SysUserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    RedisTemplate redisTemplate;

    @Cacheable(value = "users",key = "'user::'+#id")
    @Override
    public SysUser getUser(Long id) {
        return sysUserMapper.selectById(id);
    }


    @CachePut(value = "users",key = "'user::'+#sysUser.getUsrId()")
    @Override
    public SysUser insert(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
        return sysUser;
    }
    @CachePut(value = "users",key = "'user::'+#sysUser.getUsrId()")
    @Override
    public SysUser update(SysUser sysUser) {
        sysUserMapper.updateById(sysUser);
        return sysUser;
    }

    @CacheEvict(value = "users",key = "'user::'+#id")
    @Override
    public int delete(Long id) {
        return sysUserMapper.deleteById(id);
    }

//    @Override
//    public SysUser getUser(Long id) {
//        SysUser sysUser =(SysUser) redisTemplate.opsForValue().get("user::" + id);
//        if (sysUser==null){
//             sysUser = sysUserMapper.selectById(id);
//            redisTemplate.opsForValue().set("user::" + id,sysUser);
//        }
//        return sysUser;
//    }
}
